import requests
from common.logging_tool import init_log_config
import logging
import jsonschema


class TestIhrmLogin:
    init_log_config("./ihrm_login.log")

    def test_login_ok(self):
        req_url = "http://ihrm-java.itheima.net/api/sys/login"
        logging.info(f"登录员工接口的url=={req_url}")
        req_body = {"mobile": "13800000001", "password": "123456"}
        res = requests.post(url=req_url, json=req_body)
        logging.info(f"响应数据=={res.json()}")
        token = "Bearar " + res.json()["data"]
        logging.info(f"令牌=={token}")

        assert res.status_code == 200
        assert res.json()["success"] == True
        assert res.json()["code"] == 10000
        assert "成功" in res.json()["message"]

        j_schema = {
            "type": "object",
            "properties": {
                "status_code":{"const":200},
                "success": {"const": True},
                "code": {"type": "integer"},
                "message": {"pattern": "成功"}
            }
        }
        data = {
            "status_code": 200,
            "success": True,
            "code": 10000,
            "message": "操作成功！",
            "data": ""
        }

        res = jsonschema.validate(schema=j_schema, instance=data)
        print('校验结果====', res)